#pragma once

#include "checker_board.h"
//#include "checker_ui.h"

#include <iostream>

class checker_game
{
public:
	checker_game()
		: no_legal_move_black(false)
		, no_legal_move_white(false)
	{}

	void start();

	void movement(PIECE_TYPE piece_type, bool random);
	void display_board();
	void display_result();

	bool is_closed();

	int get_input_for_x(std::string& turn);
	int get_input_for_y(std::string& turn);

private:
	checker_board	board_;
	//checker_ui		ui_;

	bool no_legal_move_black,no_legal_move_white;
};


int checker_game::get_input_for_x(std::string& turn)
{
	char x = 0;

	do
	{
		std::cout << "[" << turn.c_str() << "] x(a-h) : ";
		std::cin >> x;
		x = tolower(x);
	}
	while(x < 'a' || x > 'h');

	return x - 'a';
}

int checker_game::get_input_for_y(std::string& turn)
{
	int y = 0;

	do
	{
		std::cout << "[" << turn.c_str() << "] y(1-8) : ";
		std::cin >> y;
	}
	while(y < 1 || y > 8);

	return y - 1;
}

void checker_game::movement(PIECE_TYPE side, bool random)
{
	std::string turn = (side == WHITE)? "WHITE":"BLACK";

	checker_piece* piece = NULL;
	COORD next_x, next_y;

	bool moved = false;
	bool legal_move = board_.check_legal_and_auto_movement(side, !random, &moved);
	if( false == legal_move )
	{
		if( side == WHITE )
			no_legal_move_white = true;
		else
			no_legal_move_black = true;
		
		std::cout << "[" << turn.c_str() << "] no legal move" << std::endl;
		return ;
	}

	if( true == moved )
	{
		std::cout << "[" << turn.c_str() << "] a piece moved automatically" << std::endl;
		return ;
	}

	do 
	{
		std::cout << "[" << turn.c_str() << "] enter coordinates of piece which you want to move" << std::endl;
		COORD x = get_input_for_x(turn);
		COORD y = get_input_for_y(turn);

		piece = board_.get_piece(x, y);
		if( NULL == piece || side != piece->get_type() )
			std::cout << "[" << turn.c_str() << "] no " << turn.c_str() << " piece found - Enter again" << std::endl;
		else
			break;
	}
	while(true);

	do 
	{
		std::cout << "[" << turn.c_str() << "] enter coordinates of where you want to move the piece?" << std::endl;
		next_x = get_input_for_x(turn);
		next_y = get_input_for_y(turn);

		checker_piece* destination = board_.get_piece(next_x, next_y);
		if( NULL != destination )
			std::cout << "[" << turn.c_str() << "] already a piece there" << std::endl;
		else
		{
			if ( ! board_.move(piece, next_x, next_y) )
			{
				std::cout << "[" << turn.c_str() << "] invalid movemenet" << std::endl;
			}
			else
				break;
		}
	}
	while(true);
}

bool checker_game::is_closed()
{
	if( no_legal_move_black || no_legal_move_white )
		return true;
	else
		return false;
}

void checker_game::display_board()
{
	board_.display();
}

void checker_game::display_result()
{
	if( no_legal_move_black && no_legal_move_white )
		std::cout << "[RESULT] Draw" << std::endl;
	else
	{
		if( no_legal_move_black )
			std::cout << "[RESULT] WHITE win" << std::endl;
		else if( no_legal_move_white )
			std::cout << "[RESULT] BLACK win" << std::endl;
	}
}

void checker_game::start()
{
	while(!is_closed())
	{
		display_board();

		movement(WHITE, false); // you can set true for second parameter to enjoy AI vs AI.

		display_board();

		movement(BLACK, true);
	}

	display_result();
}